12. Array Manipulation

Array Manipulation

Question:

Once again, let's draw an exercise from Intro to Computer Science.

If you are given an array of numbers, can you increase the value of the last number in the array by one?

Check out the MDN documentation on arrays for help.

Start Quiz:

var sampleArray = [0,0,7];

var incrementLastArrayElement = function(_array) {
    var newArray = [];
    // Your code should make newArray equal to an array that has the same
    // values as _array, but the last number has increased by one.
    
    // For example:
    // _array = [1, 2, 3];
    // turns into:
    // newArray = [1, 2, 4];
    
    // Your code goes in here!
    
    
    // Don't delete this line!
    return newArray;
};

// Did your code work? The line below will tell you!
console.log(incrementLastArrayElement(sampleArray));

User's Answer:

(Note: The answer done by the user is not guaranteed to be correct)

var sampleArray = [0,0,7];

var incrementLastArrayElement = function(_array) {
    var newArray = [];
    // Your code should make newArray equal to an array that has the same
    // values as _array, but the last number has increased by one.
    var newArray = _array;
    // For example:
    // _array = [1, 2, 3];
    // turns into:
    // newArray = [1, 2, 4];
    
    // Your code goes in here!
    var lastIndex = newArray.length - 1;
    newArray[lastIndex] = newArray[lastIndex] + 1;
    
    // Don't delete this line!
    return newArray;
};

// Did your code work? The line below will tell you!
console.log(incrementLastArrayElement(sampleArray));
Solution:

There are a few ways of solving this problem. Here's the code I wrote. Let's break it down.

function incrementLastArrayElement(_array)  {
    var newArray = [];
    newArray = _array.slice(0);
    var lastNumber = newArray.pop();
    newArray.push(lastNumber + 1);
    return newArray;
}

newArray = _array.slice(0);

Just like string.slice(begin, [end]) separates the characters of a string, array.slice(begin, [end]) separates the elements of an array from the index of the begin up to but not including end. We want to make a copy of the original array, so we won't include an [end]. At this point, newArray is a copy of the original _array.

var lastNumber = newArray.pop();

The array.pop() method conveniently gives us (or returns) the last element of the array, which in this case is the number we want to increase by 1. However! Be careful because array.pop() actually removes the last element of the array. This is why we made a copy in the previous line, so we wouldn't modify the original _array.

newArray.push(lastNumber + 1);

Just how the jQuery .append() method adds an element to the end of an HTML block, the array.push() method adds an element to the end of an array. Here, we're .push()ing the lastNumber + 1, which is exactly what we wanted to do.

And with that, we've got our newly incremented array!

INSTRUCTOR NOTE:

In this quiz you are given an array of numbers. Can you increase the value of the last number in the array by one?

Check out the MDN documentation on arrays for help.

Hint: you might consider using array.pop() and array.push().

Follow your instructors!

@cwpittman

+jameswilliams